<?php

namespace app\model;

use think\Model;

class BaseModel extends Model
{
    protected $indexAjaxParams;

    public function indexAjax($params)
    {
        $data = [];
        $this->indexAjaxParam($params);
        extract($this->indexAjaxParams);
        $model = $this->alias($alias)
            ->field($field)
            ->join($join);

        if($where){
            $model->where($where)->select();
        }

        if($order){
            $model->order($order);
        }

        if($count){
            $data[] = $model->count();
        }

        if($page_status ){
            if($select){
                $data[] = $model->page($page,$limit)->select();
            }else{
                $data[] = $model->page($page,$limit)->find();
            }
        }else{
            if($select){
                $data[] = $model->select();
            }else{
                $data[] = $model->find();
            }
        }
        return $data;
    }

    public function indexAjaxParam($params)
    {
        if(isset($params['page'])){
            $this->indexAjaxParams['page'] = $params['page'] ?? 1;
            unset($params['page']);
        }
        if(isset($params['limit'])){
            $this->indexAjaxParams['limit'] = $params['limit'] ?? 10;
            unset($params['limit']);
        }
        $this->indexAjaxParams['where'] = array_merge($this->indexAjaxParams['where'],$params);
    }


}